<?php
/**
* @file Darenm.php
* @synopsis  达人
* @author Change, <change@medical-lighter.com>
* @version 1.0
* @date 2015年8月29日14:27:06
*/

defined('BASEPATH') OR exit('No direct script access allowed');

class Darenm extends CI_Model
{
	function __construct()
	{
		parent::__construct();
		$this->load->Model("userm");
		$this->load->model('tmm');
	}

	public function get_list_by_thread($uid,$thread,$start=0,$limit=10,$only_expert=0)
	{
		//如果是 全科 则不用区分科室
		$rand = rand(0, 100) / 100;
        $this->db->select("ml_daren.uid")->from("ml_daren");
        $this->db->join("ml_fellow","ml_fellow.id=ml_daren.uid","left");
        if(!in_array($thread, array("-1","-2")))
        {
            $this->db->where("ml_fellow.thread",$thread);
        }
        $this->db->where('status',1);
		$only_expert = 1;
        if($only_expert == 1)
        {
            $this->db->where("ml_fellow.user_level",5);
        }
        $this->db->limit($limit,$start);
		$query_type = $this->db->order_by($rand)->get()->result_array();

		$data = array();
		
		foreach($query_type AS $row)
		{
			$userinfo = $this->userm->get_one_user_4_search($row['uid'],$uid);
			if(($userinfo['follow_status'] == 0 || $userinfo['follow_status'] == 2) && $row['uid'] != $this->uid)  //只要未关注才能显示
			{
				$data[] = $userinfo;	
			}
		}
		return $data;
	}

    public function get_list_by_city($uid,$city)
    {
		$rand = rand(0, 100) / 100;
        $this->db->select("id")->from("ml_fellow");
        $this->db->where('verified_status',3);
        $this->db->where('is_show',1);
        $this->db->like("practice_hospital",$city);
        $this->db->limit(10,0);
		$query_type = $this->db->order_by($rand)->get()->result_array();

        $data = array();
        foreach($query_type AS $row)
        {
            $userinfo = $this->userm->get_one_user_4_search(strval($row['id']),strval($uid));
            if(($userinfo['follow_status'] == 0 || $userinfo['follow_status'] == 2) && $row['id'] != $this->uid)  //只要未关注才能显示
            {
                $data[] = $userinfo;
            }
        }
        return $data;
    }

	/**
	 * 获取本地同科室 异地同科室的相关用户
	 * @param $uid
	 * @param $city
	 * @param $thread
	 * @param int $local 本地 1 异地 0
	 */
	public function get_user_list_by_city($uid, $city, $thread, $local = 1)
	{
		$rand = rand(0, 100) / 100;
		$filter = array('verified_status'=>3, 'is_show'=>1, 'thread' => $thread,'user_level'=>0);
		if ($local) {
			$filter['city'] = $city;
		} else {
			$filter['city !='] = $city;
		}

		$list = $this->db
			->select('id')
			->from('ml_fellow')
			->where($filter)
			->limit(10,0)
				->order_by($rand)
			->get()
			->result_array();
		$data = array();
		foreach($list as $k => $v) {
			$userinfo = $this->userm->get_one_user_4_search(strval($v['id']),strval($uid));
			$thread   = explode(' ',$userinfo['specialty_name']);
			$userinfo['specialty_name'] = $thread[0];
			if(($userinfo['follow_status'] == 0 || $userinfo['follow_status'] == 2) && $v['id'] != $this->uid)  //只要未关注才能显示
			{
				$data[] = $userinfo;
			}
		}
		return $data;
	}

	/* 获取普通用户 */

	/**
	 * 获取分类推荐用户列表（普通+推荐）
	 * @param $thread
	 */
	public function get_user_list_by_thread($thread, $page, $page_size, $uid)
	{
		$list = array();
		$list['normal'] = $this->get_normal_list($thread);
		$list['special'] = $this->get_special_list($thread);
		$data = array();
		foreach ($list as $k => $v) {
			foreach ($v as $j => $q) {
				$userinfo = $this->userm->get_one_user_4_search(strval($q['id']), strval($uid));
				$thread   = explode(' ',$userinfo['specialty_name']);
				$userinfo['specialty_name'] = $thread[0];
				if (($userinfo['follow_status'] == 0 || $userinfo['follow_status'] == 2) && $q['id'] != $uid)  //只要未关注才能显示
				{
					$data[$k][] = $userinfo;
				}
			}
		}
		return $data;
	}

	/* 获取专家用户 */

	public function get_normal_list($thread)
	{
		$key  = DAREN_BY_THREAD.$thread;
		$res  = unserialize($this->redis->get($key));
		return $res;
	}

	public function get_special_list($thread)
	{
		$rand = rand(0, 100) / 100;
		$this->db
				->select('a.uid as id')
				->from('ml_daren as a')
				->join('ml_fellow as b','b.id=a.uid','left');
		if(!in_array($thread, array("-1","-2")))
		{
			$this->db->where("b.thread",$thread);
		}
		$this->db->where(array('a.status'=>1,'b.user_level'=>5));
		$this->db->limit(10,0);
		return $this->db->order_by($rand)->get()->result_array();
	}

	/* 根据标签获取推荐用户列表 */

	public function get_recommend_by_tag_list($tag_id, $page, $page_size, $dis_cate_flag)
	{
		$start = ($page-1)*$page_size;
		$res = $this->db
				->select('a.user_id,b.user_level')
				->from('ml_tag_type as a')
				->join('ml_fellow as b','b.id = a.user_id','left')
				->where(array('a.tag_id' => $tag_id, 'a.type' => $dis_cate_flag, 'a.is_show' => 1))
				->group_by('a.user_id')
				->order_by('a.weight', 'desc')
				->limit($page_size,$start)
				->get()
				->result_array();
		$user_ids = array();
		if(!$res){
			return false;
		}
		foreach($res as $v) {
			$user_ids[] = $v['user_id'];
		}
		foreach($user_ids as $k => $v) {
			$userinfo = $this->userm->get_one_user_4_search(strval($v),strval($this->uid));
			if(($userinfo['follow_status'] == 0 || $userinfo['follow_status'] == 2) && $v != $this->uid)  //只要未关注才能显示
			{
				$data[] = $userinfo;
			}
		}
		return $data;
	}

    //取专家
    public function get_expert_list_by_city($uid, $city, $thread, $local = 1)
    {
		$rand = rand(0, 100) / 100;
        $filter = array('user_level'=>5, 'is_show'=>1, 'thread' => $thread);
        if ($local) {
            $filter['city'] = $city;
        } else {
            $filter['city !='] = $city;
        }

        $list = $this->db
            ->select('id')
            ->from('ml_fellow')
            ->where($filter)
            ->limit(5,0)
				->order_by($rand)
            ->get()
            ->result_array();

        $data = array();
        foreach($list as $k => $v) {
            $userinfo = $this->userm->get_one_user_4_search(strval($v['id']),strval($uid));
			$thread   = explode(' ',$userinfo['specialty_name']);
			$userinfo['specialty_name'] = $thread[0];
            if(($userinfo['follow_status'] == 0 || $userinfo['follow_status'] == 2) && $v['id'] != $this->uid)  //只要未关注才能显示
            {
                $data[] = $userinfo;
            }
        }
        return $data;
    }

	/**
	 * 获取同医院的用户
	 * @param $uid
	 * @param $hospital
	 */
	public function get_user_list_by_hospital($uid, $hospital)
	{
		$rand = rand(0, 100) / 100;
		$filter = array('verified_status'=>3, 'is_show'=>1, 'practice_hospital' => $hospital,'user_level'=>0);

		$list = $this->db
			->select('id')
			->from('ml_fellow')
			->where($filter)
			->limit(10,0)
				->order_by($rand)
			->get()
			->result_array();

		$data = array();
		foreach($list as $k => $v) {
			$userinfo = $this->userm->get_one_user_4_search(strval($v['id']),strval($uid));
			$thread   = explode(' ',$userinfo['specialty_name']);
			$userinfo['specialty_name'] = $thread[0];
			if(($userinfo['follow_status'] == 0 || $userinfo['follow_status'] == 2) && $v['id'] != $this->uid)  //只要未关注才能显示
			{
				$data[] = $userinfo;
			}
		}
		return $data;
	}

    public function get_expert_list_by_hospital($uid, $hospital)
    {
        $filter = array('user_level'=>5, 'is_show'=>1, 'practice_hospital' => $hospital);

        $list = $this->db
            ->select('id')
            ->from('ml_fellow')
            ->where($filter)
//            ->limit(5,0)
//            ->order_by('rand()')
            ->get()
            ->result_array();

        $data = array();
        foreach($list as $k => $v) {
            $userinfo = $this->userm->get_one_user_4_search(strval($v['id']),strval($uid));
			$thread   = explode(' ',$userinfo['specialty_name']);
			$userinfo['specialty_name'] = $thread[0];
            if(($userinfo['follow_status'] == 0 || $userinfo['follow_status'] == 2) && $v['id'] != $this->uid)  //只要未关注才能显示
            {
                $data[] = $userinfo;
            }
        }
        return $data;
    }


    /**
     * 获取邀请活跃达人
     * @param array $thread_ids
     * @param int $page
     * @param int $per_page
     * @return array
     */
	public function get_invite_active_list($thread_ids = array(), $page = 1, $per_page = 20)
    {
        $user_list = array();
		$result    = array();
        foreach ($thread_ids as $k=>$t)
		{
			$res =  $this->get_daren_list_bythread($t);
			if(is_array($res))
			{
			    $result = array_merge($result,$res);
			}
		}

		if (!$result) return $user_list;

        foreach($result as $v) {
			$user_info = $this->userm->getUserInfo($v['id']);
            if ($user_info) {
                $user_info['expert_info'] = isset($user_info['expert_info'][0]['content']) ? $user_info['expert_info'][0]['content'] : '';
				$user_info['invite_thread'] = $user_info['thread_name_only'];
				unset($user_info['thread_name_only']);
                $user_list[] = $user_info;
            }
        }
        return $user_list;
    }
	/**
	 * 通过科室id获取科室达人
	 */
	public function get_daren_list_bythread($thread)
	{
		$key = DAREN_BY_THREAD.$thread;
		$res = unserialize($this->redis->get($key));
		return $res;
	}
}
